<?php
  include_once dirname(__FILE__)."/../database/database.class.php";
  include_once dirname(__FILE__)."/../classes/card.class.php";

  $to_encode = array();

  if(isset($_GET['type']) && isset($_GET['id']))
  {

    if(!isset($_GET["PAGE"]))
    {
      $_GET["PAGE"] = 0;
    }
    if(!isset($_GET["PSIZE"]))
    {
      $_GET["PSIZE"] = 10000;
    }

    $order_focus = "`Card`.`cardnumber`";
    $order_direction = "ASC";
    if(isset($_GET["OFOCUS"]) && $_GET["OFOCUS"] == "name")
    {
      $order_focus = "`Card`.`name`";
    }

    if(isset($_GET["ODIR"]) && $_GET["ODIR"] == "DESC")
    {
      $order_direction = "DESC";
    }

    if($_GET['type'] == "expansion")
    {
      include_once(dirname(__FILE__)."/../classes/expansion.class.php");
      $expansion = new Expansion($_GET['id']);
      $cards = $expansion->getCards(array(array('column' => $order_focus, 'direction' => $order_direction)), array('page' => $_GET["PAGE"], 'pagelimit' => $_GET["PSIZE"]));
    }
    else if($_GET['type'] == "deck")
    {
      include_once dirname(__FILE__)."/../classes/deck.class.php";
      $deck = new Deck($_GET['id']);
      $cards = $deck->getCards(array(
                                array('column' => "`Card`.`cardnumber`",
                                      'direction' => "ASC")
                               ),
                               array('page' => $_GET["PAGE"], 'pagelimit' => $_GET["PSIZE"])
                              );
    }
    else if($_GET['type'] == "collection")
    {
      include_once dirname(__FILE__)."/../classes/collection.class.php";
      $collection = new Collection($_GET['id']);
      $cards = $collection->getCards(array(
                                array('column' => "`Card`.`cardnumber`",
                                      'direction' => "ASC")
                               ),
                               array('page' => $_GET["PAGE"], 'pagelimit' => $_GET["PSIZE"])
                              );
    }
    else
    {
      return;
    }

    foreach($cards as $card)
    {
      $card_obj = $card;

      $expansionInternalName = $card_obj->getExpansion("internalname");

      $ending = "*";
      
      $tempcard = new stdClass();
      $tempcard->id = $card->getID();
      $tempcard->name = $card->getCardname();
      $tempcard->src = $card_obj->getImageURL();
      $tempcard->url = "card.php?cardid=".$card->getID();
      array_push($to_encode,$tempcard);
      unset($tempcard);
    }
  }

  echo json_encode($to_encode);
?>
